﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;
using TowerDefence_Zombie.Data;

namespace TowerDefence_Zombie
{
    public abstract class EntityTowerAttacker : EntityTowerBase
    {
        private Targetter targetter;
        private Attacker attacker;

        protected override void OnInit(object userData)
        {
            base.OnInit(userData);
            targetter = transform.Find("Targetter").GetComponent<Targetter>();
            attacker = transform.Find("Attack").GetComponent<Attacker>();

            targetter.OnInit(userData);
            attacker.OnInit(userData);
        }

        protected override void OnShow(object userData)
        {
            base.OnShow(userData);
            attacker.SetOwnerEntity(Entity);
        }

        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(elapseSeconds, realElapseSeconds);

            if (pause) return;

            targetter.OnUpdate(elapseSeconds, realElapseSeconds);
            attacker.OnUpdate(elapseSeconds, realElapseSeconds);
        }

        protected override void OnHide(bool isShutdown, object userData)
        {
            base.OnHide(isShutdown, userData);
            targetter.OnHide(isShutdown, userData);
            attacker.OnHide(isShutdown, userData);
            attacker.EmptyOwnerEntity();
        }

        protected override void OnShowTowerLevelSuccess(Entity entity)
        {
            base.OnShowTowerLevelSuccess(entity);

            EntityTowerLevel entityTowerLevel = entity.Logic as EntityTowerLevel;
            targetter.SetAlignment(Alignment);
            targetter.SetTurret(entityTowerLevel.turret);
            targetter.SetSearchRange(entityDataTower.Tower.Range);
            targetter.ResetTargetter();
            targetter.SetEntityTowerLevel(entityTowerLevel);

            AttackerData attackerData = AttackerData.Create(entityDataTower.Tower.Range,
                entityDataTower.Tower.FireRate,
                entityDataTower.Tower.IsMultiAttack,
                entityDataTower.Tower.ProjectileType,
                entityDataTower.Tower.ProjectileEntityId);

            ProjectileData projectileData = new(entityDataTower.Tower.Damage,
                entityDataTower.Tower.SpeedDownRate,
                entityDataTower.Tower.ContinuousDamage);

            attacker.SetData(attackerData, projectileData);
            attacker.SetTargetter(targetter);
            attacker.SetProjectilePoints(entityTowerLevel.projectilePoints);
            attacker.SetEpicenter(entityTowerLevel.epicenter);
            attacker.SetLaunch(entityTowerLevel.launcher);
            attacker.ResetAttack();

            targetter.OnShow(null);
            attacker.OnShow(null);
        }

        protected override void ProcTowerStarInfo()
        {
            base.ProcTowerStarInfo();
            if (targetter != null)
            {
                targetter.SetSearchRange(entityDataTower.Tower.Range);
                Log.Info($"星级 - 攻击范围处理 - 总范围{entityDataTower.Tower.Range}");
            }
            if (attacker != null
                && attacker.attackerData != null
                && attacker.projectileData != null)
            {
                attacker.attackerData.Range = entityDataTower.Tower.Range;
                attacker.attackerData.FireRate = entityDataTower.Tower.FireRate;
                attacker.projectileData.Damage = entityDataTower.Tower.Damage;
                attacker.projectileData.SlowRate = entityDataTower.Tower.SpeedDownRate;
                attacker.projectileData.ContinuousDamage = entityDataTower.Tower.ContinuousDamage;

                Log.Info($"星级 - 范围处理 - 范围{ attacker.attackerData.Range}");
                Log.Info($"星级 - 攻速处理 - 攻速{ attacker.attackerData.FireRate}");
                Log.Info($"星级 - 伤害处理 - 伤害{ attacker.projectileData.Damage}");
                Log.Info($"星级 - 减速处理 - 减速{ attacker.projectileData.SlowRate}");
                Log.Info($"星级 - 毒素处理 - 毒素{ attacker.projectileData.ContinuousDamage}");
            }
        }
    }
}

